<?php
class View_Bug_Manager_Edit extends CMF_Krypton_Core_View
{
	public function buildContent()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );
		$graph		= $this->registry->get( 'graph' );
		$words		= $this->words['bug']['edit'];

		if( $bug_id	= $request->get( 'bug_id' ) )
		{
			$bug	= new Bug( $bug_id );
			$data	= $bug->getData( TRUE );
			if( count( $data ) )
			{
				$assign	= new Assignment();
				$assign->focusForeign( "bug_id", $bug_id );
				$assignments	= $assign->getData( false );
				if( !count( $assignments ) && !$bug->isOwner( $auth->getCurrentUser() ) )
				{
					$content	= $this->buildContentView();
					return $content;
				}
				else
				{
					$user	= new User();
					if( !$user->hasRole( $auth->getCurrentUser(), 2 ) && 0 )		//  is a Manager
					{
						$content	= $this->buildView();
						$content	.= $this->buildMessage();
						return $content;
					}
					else
					{
						$user	= new User( $data['author'] );
						$author	= $user->getUsername();

						//  --  SOURCES --  //
						$opt_project = array();
						$server	= new Server();
						$list		= $server->getProjects();
						foreach( $list as $id	=> $serverproject )
							$opt_project[$id] = $serverproject;
						$opt_project['_selected']	= $data['server'].":".$data['project'];

						//  --  PRIORITIES  --  //
						$opt_priority = $this->words['main']['priorities'];
						$opt_priority['_selected']	= $data['priority'];

						//  --  STATES  --  //
						$opt_status	= array();
						foreach( $this->words['main']['states'] as $key => $word )
							if( $graph->isEdge( new ADT_Tree_Node( $data['status'] ), new ADT_Tree_Node( $key ) ) )
								$opt_status[$key] = $word;
						$opt_status['_selected']	= $data['status'];

						//  --  TYPES  --  //
						$opt_type	= array();
						foreach( $this->words['main']['types'] as $key => $word )
							$opt_type[$key] = $word;
						$opt_type['_selected']	= $data['type'];

						//  --  DEVELOPER ASSIGNMENTS  --  //
						$assign			= new Assignment();
						$assign->focusForeign( "bug_id", $bug_id );
						$devs			= $assign->getData( FALSE );
						$dev_list			= array();
						foreach( $devs as $dev )
						{
							$user		= new User( $dev['user_id'] );
							$user		= $user->getData( TRUE );
							$dev_list[]	= $user['username'];
						}
						$developers		= implode( ", ", $dev_list );

						$data['author']		= $author;
						$data['created']	= date( "d.m.y H:i:s", $data['created'] );
						$data['modified']	= date( "d.m.y H:i:s", $data['modified'] );
						$data['developers']	= $developers;
						$data['status']		= $this->words['main']['states'][$data['status']];

						$sources	= array(
							'opt_project'	=> $opt_project,
							'opt_priority'	=> $opt_priority,
							'opt_status'	=> $opt_status,
							'opt_type'	=> $opt_type,
							);

						//  --  COMPONENTS  --  //
						$ui	= $this->buildForm( 'bug', 'editBug', 'bug', 'edit', $data, $sources );
						$link_bug_id		= $this->html->Link( "?link=bug&bug_id=".$data['bug_id'], $words['link_bug'] );

						$button_cancel	= $this->html->LinkButton( "?link=".$request->get( 'link' ), $words['button_cancel'], 'but cancel' );
						$button_edit	= $this->html->Button( 'editBug', $words['button_edit'], 'but edit' );
						$ui['field_button_cancel']	= $this->html->Field( '', $button_cancel );
						$ui['field_button_edit']	= $this->html->Field( '', $button_edit );

						$ui['field_bug_id']		= $this->html->Field( '', "<b>".$data['bug_id']."</b> ".$link_bug_id );

						//  --  ATTACHMENTS  --  //
						$list			= array();
						$attachments	= new Attachment();
						$attachments->focusForeign( "bug_id", $bug_id );
						$data		= $attachments->getData();
						if( count( $data ) )
						{
							foreach( $data as $attachment )
							{
								$uri		= $config['paths']['attachments'].$attachment['filename'];
								$title	= $attachment['title'];
								if( $attachment['description'] )
									$title	= $this->html->Acronym( $attachment['title'], $attachment['description'] );
								$list[]	= "<li>".$this->html->Link( $uri, $title, '', '_blank' )."</li>";
							}
							$attachments	= implode( "", $list );
						}
						else
							$attachments	= $words['no_attachments'];

						$ui['label_attachments']	= $this->html->Label( '', $words['attachments'] );
						$ui['field_attachments']	= $this->html->Field( '', "<ul style='list-style:none'>".$attachments."</ul>" );

						//  --  COMMENTS  --  //
						$user		= new User;
						$list			= array();
						$_comments	= new Comment();
						$_comments->focusForeign( "bug_id", $bug_id );
						$comments	= $_comments->getData( FALSE );
						foreach( $comments as $comment )
						{
							$user	= new User( $comment['author'] );
							$author	= $user->getUsername();
							$text	= nl2br( $comment['comment'] );
							$text	= $this->includeBugLink( $text );
							$status	= $comment['status'] ? "<br/><b>[".$this->words['main']['states'][$comment['status']]."]</b>" : "";
							$label	= $author."<br/>".date( "d.m.y H:i:s", $comment['timestamp'] ).$status;
							$list[]	= "<tr>".$this->html->Label( '', $label ).$this->html->Field( '', $text )."</tr>";
						}
						$ui['comments']	= implode( "", $list );

						$input_comment	= $this->html->TextArea( 'edit_comment', '', 'xm' );
						$ui['label_comment']	= $this->html->Label( 'comment', $words['comment'] );
						$ui['field_comment']	= $this->html->Field( 'comment', $input_comment );

						//  --  BUG PAGING  --  //
						$ui['button_previous']	= "<button class='buttiny' disabled='disabled'><<</button>";
						$ui['button_next']		= "<button class='buttiny' disabled='disabled'>>></button>";

						if( $prev = $bug->getPreviousBug( $bug_id ) )
							$ui['button_previous']		= "<button class='buttiny' onClick=\"document.location.href='"."?link=".$request->get( 'link' )."&bug_id='+".($prev).";\"><<</button>";
						if( $next = $bug->getNextBug( $bug_id ) )
							$ui['button_next']			= "<button class='buttiny' onClick=\"document.location.href='"."?link=".$request->get( 'link' )."&bug_id='+".($next).";\">>></button>";

						$data	= $bug->getData( TRUE );

						$ui['input_bug_id']		= $this->html->Input( 'bug_id', $bug_id, 'inbit' );
						$ui['button_bug_id']	= $this->html->Button( 'setBug', '>', 'butbit' );
						$ui['button_bug_id']	= "<button class='butbit' onClick=\"document.location.href='"."?link=".$request->get( 'link' )."&bug_id='+document.getElementById('bug_id').value;\">></button>";

						$ui['form']		= $this->html->Form( 'editBug', "?link=".$request->get( 'link' )."&bug_id=".$request->get( 'bug_id' ) );
						$ui['colgroup']		= $this->html->ColumnGroup( "30%", "70%" );
						$ui['caption']		= $this->html->TableCaption( $words['caption'] );
						$ui['heading']		= str_replace( '{#title#}', $data['title'], $words['heading'] );

						$content	= $this->loadTemplate( 'bug.edit', $ui );
						return $content;
					}
				}
			}
			else
				$this->messenger->noteError( "Bug is not existing." );
		}
		else
			$this->messenger->noteError( $words['msg_error1'] );
	}
}
?>